#!/bin/bash


echo "-***********************************-"
echo "超牛X HDFS落地日志行数统计脚本启动"
echo "-***********************************-"

dt=$(date -d'-1 day' +%Y-%m-%d)

if [ $1 ]
then
dt=$1
fi

echo "统计的日期为: $dt"



# 加载要统计的采集落地日期目录到一个hive的临时表
hive -e "alter table tmp.log_collection_tmp add partition(dt='${dt}') location '/deepsea/user_action_logs/${dt}'"

# 执行hivesql，统计当天的日志行数，并截取输出结果
# line_amount 8
x=$(hive -e "select 'line_amount',count(1) from tmp.log_collection_tmp where dt='${dt}'" | grep line_amount | grep -v select)

# 从输出结果中截取 行数 8
line_count=$(echo $x | cut -d' ' -f 2)

# 统计hdfs落地目录中的文件个数
file_cnt=$(hdfs dfs -ls /deepsea/user_action_logs/${dt} | grep -v Found | wc -l)


json_dt="\"statisticDate\"":"\"$dt\""
json_file="\"logFileCount\"":$file_cnt
json_line="\"logLineAmount\"":$line_count

param="{$json_dt ,$json_file ,$json_line}"

echo "最终结果如下:"
echo $param

curl -X POST -H 'Content-Type:application/json;utf-8' -H 'Accept:application/json' -d "$param" http://192.168.77.2:8080/api/log_hdfs_statistic_report